#include "CCD290.h"

int main( int argc, char* argv[] ){

    if( argc != 2 && argc != 3 ){
        printf("usage:\n\t%s addr\n", argv[0]);
        printf("or:\n\t%s addr reg_val\n", argv[0]);
        // printf("usage:\n\t%s addr\n", argv[0]);
        return 0;
    }

    SPI_CONFIG *spi_config = (SPI_CONFIG*)malloc(sizeof(SPI_CONFIG));
    setup_SPI_by_device_name(spi_config, "ad9106");

    uint addr = (uint)atoi(argv[1]);
    uint val = 0;

    if( argc == 3 ){
        val = (uint)atoi(argv[2]);
    }

    int fd = open("/dev/mem", O_RDWR | O_SYNC);
	uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
	uint *ptr2 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_IN);

	uint *gpio_reg 		= ptr1;		// 0x41200000
	uint *gpio2_reg		= ptr1+2;	// 0x41200008
	uint *gpio_in_reg	= ptr2;		// 0x41210000

    uint data = (addr << 16) + val;

    if( argc == 3 ){
        send_spi_data_32bits(   gpio_reg, 
                                gpio2_reg, 
                                gpio_in_reg, 
                                spi_config,
                                data );

        // ad9106_ramupdate(   gpio_reg,
        //                     gpio2_reg,
        //                     gpio_in_reg,
        //                     spi_config);
    }

    uint READ_BIT = 0x8000;
	data = (addr | READ_BIT) << 16;
	send_spi_data_32bits( gpio_reg, 
						  gpio2_reg, 
						  gpio_in_reg, 
						  spi_config,
						  data );

    free(spi_config);
    munmap(ptr1,10);
	munmap(ptr2,10);

	return 0;
}
